package com.lucker.framework.filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestFilter implements Filter {

	private String encodeing = "UTF-8";
	private static Logger logger = LoggerFactory.getLogger(RequestFilter.class);
	
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encodeing = "UTF-8";
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		ServletRequest requestWrapper = null;  
        if(request instanceof HttpServletRequest) {  
            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request);  
        }  
        if(null == requestWrapper) {  
            chain.doFilter(request, response);  
        } else {  
            chain.doFilter(requestWrapper, response);  
        }
	}

	@Override
	public void destroy() {
		
	}
	
	
	
//	
//	class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
//		  
//	    private byte[] body;  
//	    private String encodeing = null;
//	    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {  
//	        super(request);  
//	        //body = StreamUtil.readBytes(request.getReader(), JoddDefault.encoding);  
//	        
//	        encodeing = request.getCharacterEncoding();
//	        StringBuffer stringBuffer = new StringBuffer();
//	        BufferedReader bufferedReader = null;
//	        
//	        
//	        try {
//	        	bufferedReader = this.getRequest().getReader();
//	        	String val = null;
//	        	while((val = bufferedReader.readLine()) != null) {
//	        		stringBuffer.append(val);
//	        	}
//	        	body = stringBuffer.toString().getBytes(encodeing);
//	        }catch(IOException e) {
//	        	logger.error("RequestFilter.BodyReaderHttpServletRequestWrapper异常" , e);
//	        }finally {
//				try {
//					if(bufferedReader != null) {
//						bufferedReader.close();
//					}
//				}catch(Exception e) {}
//			}
//	        
//	        
//	    }  
//	  
//	    @Override  
//	    public BufferedReader getReader() throws IOException {  
//	        return new BufferedReader(new InputStreamReader(getInputStream()));  
//	    }  
//	  
//	    @Override  
//	    public ServletInputStream getInputStream() throws IOException {  
//	        final ByteArrayInputStream bais = new ByteArrayInputStream(body);  
//	        return new ServletInputStream() {  
//	  
//	            @Override  
//	            public int read() throws IOException {  
//	                return bais.read();  
//	            }  
//	        };  
//	    }  
//	  
//	}  

}
